<template>
	<view class="page">
		<view class="institution" v-for="(item,index) in institutionList" :key="index">
			<view class="institution_title">
				{{item.name}}
			</view>
			<view class="institution_logo">
				<image :src="item.avatarUrl" mode=""></image>
			</view>
			<view class="institution_info">
				<view class="institution_content">

					{{item.insIntroduction}}
				</view>
				<view class="institution_more">
					<p style="text-align: right;padding-right: 14rpx;height: 40rpx;line-height: 40rpx;">
						<text class="more" @click="jumpToMore(item._id)">了解详情</text>
						<!-- <button type="default" size="mini" style="margin-right:14rpx;font-size: 14rpx;color: #666666;">了解详情</button> -->
					</p>
					<view>
						<view style="width: 200rpx;float: left;">
							<p>{{item.phone}}</p>
							<!-- <p>{{item.startTime}} - {{item.endTime}}</p> -->
						</view>
						<view style="width: 200rpx;float: right;">

							<button class="addButton" hover-class="button-hover" type="default" size="mini" @click="jumpToInformation(item)">加入</button>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import cloudbase from '@cloudbase/js-sdk'
	export default {
		data() {
			return {
				type:'',
				institutionList: []
			}
		},
		onLoad(option) {
			this.type = option.type;
		},
		created() {
			this.getInstitution();
		},
		methods: {
			getInstitution:function(){
				cloudbase.database().collection('institution').get().then(res=>{
					// console.log(res.data);
					let list = res.data;
					if(list.length!=0){
						let bufferList = [];
						list.forEach(v => {
							if (v.auth == true) {
								if (!v.avatarUrl) {
									v.avatarUrl = 'cloud://inspace-v1.696e-inspace-v1-1302892928/institutionImage/logo2.png'
								}
								// 获取通用链接
								cloudbase.getTempFileURL({
									fileList: [v.avatarUrl]
								}).then(res => {
									let list = res.fileList[0];
									v.avatarUrl = list.tempFileURL ? list.tempFileURL : v.avatarUrl;
									bufferList.push(v);
								})
							}
						})
						this.institutionList = bufferList;
					}
				})
			},
			jumpToInformation:function(item) {
				console.log(item)
				let that = this;
				if(that.type == 'register'){
					let result = item.name;
					let pages = getCurrentPages(); //获取所有页面栈实例列表
					let nowPage = pages[pages.length - 1]; //当前页页面实例
					let prevPage = pages[pages.length - 2]; //上一页页面实例
					prevPage.$vm.form.institution = item.name; //修改上一页data里面的institution 参数值
					prevPage.$vm.form.institutionId = item._id; //修改上一页data里面的institution 参数值
					getApp().globalData.userInfo.institutionId = item._id;
					// uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					// 	delta: 1
					// });
					uni.navigateTo({
						url:"../submitSuccess/submitSuccess?index=0"
					})
				}
				else{
					if(getApp().globalData.userInfo.institution){
						uni.showToast({
							title:'您已加入机构',
							icon:'none'
						})
					}else{
						cloudbase.database().collection('user').where({openId:getApp().globalData.userInfo.openId}).update({
							institution:item.name,
							institutionId:item.institutionId
						}).then(res=>{
							if(res.updated ==1){
								uni.navigateTo({
									url:"../submitSuccess/submitSuccess?index=0"
								})
							}
						})
					}
				}
			},
			
			jumpToMore:function(e){
				uni.navigateTo({
					url:'../institutionMore/institutionMore?id='+e
				})
			}
		}
	}
</script>

<style>
	.page {
		width: 750rpx;
		height: 1435rpx;
		background-color: #F3F5F7;
		display: flex;
		flex-direction: column;
	}

	.institution {
		width: 701rpx;
		height: 353rpx;
		padding: 20rpx;
		margin-top: 20rpx;
		margin-left: auto;
		margin-right: auto;
		background-color: #FFFFFF;
		border-radius: 20px;
		box-shadow: 0 5px 15px rgba(0, 0, 0, .09);
	}

	.institution_title {
		width: 100%;
		height: 40rpx;
		font-size: 35rpx;
		line-height: 40rpx;
		font-weight: 800;
	}

	.institution_logo {
		width: 144rpx;
		height: 260rpx;
		float: left;
		/* background-color: #007AFF; */
	}

	.institution_logo image {
		width: 144rpx;
		height: 144rpx;
		margin-top: 10rpx;
		border-radius: 50%;
	}

	.institution_info {
		width: 516rpx;
		height: 260rpx;
		float: left;
		font-size: 24rpx;
		padding: 20rpx;
		box-sizing: border-box;
		/* background-color: #0000FF; */
	}

	.institution_content {
		width: 476rpx;
		height: 120rpx;
		font-size: 24rpx;
		line-height: 30rpx;
		color: #666666;
		/* 多行显示+超出部分省略号替代 */
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 4;
		-webkit-box-orient: vertical;
	}

	.institution_more {
		width: 476rpx;
		height: 100rpx;
		color: #999999;
		/* background-color: #0000FF; */
	}

	.more {
		font-size: 20rpx;
		color: #898A8F;
		user-select: none;
	}

	.addButton {
		float: right;
		background-color: #FD973F !important;
		border-radius: 20px;
		color: #FFFFFF !important;
	}

	.button-hover {
		background-color: rgba(0, 0, 0, 0.1) !important;
		opacity: 0.7;
	}
</style>
